<#import "/META-INF/spring.ftl" as spring />
<#import "/META-INF/mspring.ftl" as mspring />
<#include "../inc/header.ftl" />
<script type="text/javascript" src="${base}/script/kindeditor/kindeditor.js" charset="utf-8"></script>
<script type="text/javascript" src="${base}/script/jquery.form.js" charset="utf-8"></script>
<script type="text/javascript" src="${base}/script/autocomplete.js" charset="utf-8"></script>
<script type="text/javascript">
	$(document).ready(function(){
		mlog.editor.init({
			model : "all",
			id : "content"
		});
		mlog.editor.init({
			model : "simple",
			id : "summary"
		});
	});
	
	function autosave(callback){
		$('#newsForm').ajaxSubmit({
			url : '${base}/admin/news/autosave',
			success: function(responseText, statusText, xhr, $form){
				$("#id").val(responseText);
				if(callback !== undefined){
					callback(responseText, statusText);
				}
			}
		});
		setTimeout("autosave()", 60000);
	}
	autosave();
	function displayToggle(){
		autosave(function(responseText, statusText){
			$('#attach_frame').attr('src', '${base}/common/attachment/uploadview?fkey=' + responseText + '&ftype=NEWS');
			$('#attachment').fadeToggle();
		});
	}
	
	function downloadNewsImages(){
		autosave(function(responseText, statusText){
			var content = mlog.editor.map['content'].html();
			downloadRemoteImages(responseText, "NEWS", content, function(response){
				if(response.success == true){
					mlog.editor.map['content'].html(response.data.content);
					$('#attach_frame').attr('src', '${base}/common/attachment/attachments?fkey=' + responseText + '&ftype=NEWS');
					$('#attachment').fadeIn();
				}
				else{
					alert(response.message);
				}
			});
		});
	}
	
	function addAttachFile(path,name){
		if (mlog.editor.map['content'].designMode === false){
			alert('请先切换到所见所得模式');
		} else {
			mlog.editor.map['content'].insertHtml('<span class=\"attachment\"><a target=\"_blank\" href=\"'+path+'\" >'+name+'</a></span>');
		}
	}
	
	function addAttachImage(path,id){
		if (mlog.editor.map['content'].designMode === false){
			alert('请先切换到所见所得模式');
		}else if (path != "") {
			mlog.editor.map['content'].insertHtml('<a target=\"_blank\" href=\"'+path+'\" id=\"ematt:'+id+'\"><img src=\"'+path+'\" title="点击查看原图" border=\"0\" /></a>');
		}
	}
</script>
<div id="error" class="message error" style="display:none;"></div>
<form class="form" id="newsForm" action="${base}/admin/news/create/save" method="POST">
	<@spring.bind "news" />
	<@spring.formHiddenInput path="news.status" />
	<@spring.formHiddenInput path="news.id" />
	<table class="formtable">
		<tr>
			<td class="fieldlabel" style="width:60px;">名称</td>
			<td>
				<@spring.formInput path="news.title" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入资讯标题"}}\'' />
			</td>
			<td class="fieldlabel" style="width:60px;">分类</td>
			<td>
				<@spring.formSingleSelect path="news.catalog.id" options=newsCatalogs has_default=true attributes='style="width:98%;" validate=\'{required:true, messages:{required:"请选择资讯分类"}}\''/>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel">来源站点</td>
			<td>
				<@spring.formInput path="news.sourceSite" attributes='class="textinput" style="width:98%;"' />
			</td>
			<td class="fieldlabel">来源链接</td>
			<td>
				<@spring.formInput path="news.sourceUrl" attributes='class="textinput" style="width:98%;"' />
			</td>
		</tr>
		<tr id="attachment" style="display: none;">
			<td colspan="4">
				<iframe id="attach_frame" width="100%" height="230" frameborder="0"></iframe>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:60px;">
				正文<br/>
				<a href="javascript:displayToggle();" style="font-weight: bold; color:red;">插入附件</a><br/>
				<a href="javascript:downloadNewsImages();" style="font-weight: bold; color:red;">本地化远程图片</a><br/>
			</td>
			<td colspan="3">
				<@spring.formTextarea path="news.content" attributes='class="textinput" style="width:98%;height:240px;"' />
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:60px;">摘要</td>
			<td colspan="3">
				<@spring.formTextarea path="news.summary" attributes='class="textinput" style="width:98%;"' />
			</td>
		</tr>
		<tr>
			<td colspan="4" style="text-align:center;">
				<input type="submit" class="btn" value=" 提交 " />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	$(document).ready(function(){
		//斑马线
		var tables=document.getElementsByTagName("table");
		var b=false;
		for (var j = 0; j < tables.length; j++){
			var cells = tables[j].getElementsByTagName("tr");
			//cells[0].className="color3";
			b=false;
			for (var i = 0; i < cells.length; i++){
				if(b){
					cells[i].className="color2";
					b=false;
				}
				else{
					cells[i].className="color3";
					b=true;
				};
			};
		}
		
		mlog.form.validate({
			selector : "#newsForm",
			errorLabelContainer : "#error",
			wrapper: 'li',
			onfocusout : false,
			onkeyup : false,
			onclick : false,
			success : function(){
				mlog.utils.scrollTop();
			}
		});
	});
</script>
<#include "../inc/footer.ftl" />